/*
* Copyright (c) 2008 Matt Giger http://earthbrowser.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
* ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

var _placemark_url = "http://globe.earthbrowser.com/icons/placemark_red.png";

/**
* GGeocodePane
*
* A geocoding search pane based on the GClientGeocoder
* 
*/
function GGeocodeCtl(plugin, geocoder)
{
	var self = this;
	
	with(plugin)
	try
	{
		// Get a reference to the camera for later
		var camera = getCamera();
		
		// Search results Feature container
//		var placemarks = addFeature(new FeatureSet());
		self.placemarks = new FeatureSet();
//		self.placemarks.addListener("onclick", function(e) {
//			alert("fset click");
//		});
		
		// Create a draggable input frame
//		var base = addPane(
		self.base =	new Frame({
				x:10, y:15, width:220, height:46, draggable:true,
				frameRadius:20, frameWidth:1, backColor:0xffffff, backAlpha:0.7
			})
;//		);
		
		// window resizer
		var resizer = self.base.addPane(
			new Resizer({
				x:self.base.getWidth()-14, y:self.base.getHeight()-14, binding:BIND_BOTTOM|BIND_RIGHT,
				minWidth:200, minHeight:46, maxWidth:400, maxHeight:getHeight()-100
			})
		);
		resizer.setTarget(self.base);
		
		// Search results pane
		var results = self.base.addPane(
			new Scroller({
				x:8, y:42, visible:false,
				width:self.base.getWidth()-16, height:self.base.getHeight()-54,
				frameWidth:1, vscroll:true, binding:BIND_ALL
			})
		);
		
		// Search input text field
		self.search = self.base.addPane(
			new Input({
				x:8, y:14, width:self.base.getWidth() - 80, height:18, binding:BIND_LEFT|BIND_RIGHT,
				backColor:0xffffff, borderColor:0, background:true, border:true
			})
		);
		
		// The search function
		var do_search = function() {
			
			// Set the bounds for the search based on the camera
			/*
			var cbnds = camera.getViewBounds();
			geocoder.setViewport(
				new GLatLngBounds(
					new GLatLng(cbnds.getSouth(), cbnds.getWest()),
					new GLatLng(cbnds.getNorth(), cbnds.getEast())
				)
			);
			*/
			
			// start the search
			geocoder.getLocations(self.search.getText(), function(response) {
				
				// clean out old results
				results.removeAllPanes();
//				self.placemarks.removeAllFeatures();
				
				// add new results
				if(response.Status.code == 200)
				{
					var len = response.Placemark.length;
					for(i=0;i<len;++i)
					{
						// Add a feature to the globe
						var ftr = new GSearchFeature(plugin, response.Placemark[i]);
						self.placemarks.addFeature(ftr);
						
						// Add a result to the search results pane
						var sresult = new GSearchResult(plugin, response.Placemark[i]);
						sresult.setY(i*20);
						results.addPane(sresult);
					}
					
					// resize window
					var nheight = Math.max(46, Math.min(46 + len * 20 + 8, getHeight() - self.base.getY() - 20));
					self.base.tweenProperties(0.5, {height:nheight});
					results.setVisible(true);
				}
				else
				{
					self.base.tweenProperties(0.5, {height:46});
					results.setVisible(false);
				}
			});
		};
		
		// When the Return key is hit, call the geocoder and move the camera
		self.search.addListener("oncommit", do_search);
		
		// Search button
		var sbutton = self.base.addPane(
			new Button({
				x:self.base.getWidth() - 60, y:12, text:"Search", binding:BIND_RIGHT
			})
		);
		sbutton.addListener("onclick", do_search);
		
		// dropshadows look nice
		self.base.setFilters([new DropShadowFilter(3, 45, 1 ,0.75)]);
		self.search.setFilters([new DropShadowFilter(2, 45, 1 ,0.75)]);
		sbutton.setFilters([new DropShadowFilter(2, 45, 1 ,0.75)]);
		results.setFilters([new DropShadowFilter(2, 45, 1 ,0.75)]);
	}	
	catch(err)
	{
		alert(err.toString());
	}
	
	return self;
}

/**
* GSearchResult
*
* List element for the search result list. Clicking will zoom camera to location.
*/
function GSearchResult(plugin, result)
{
	with(plugin)
	{
		var loc = result.Point.coordinates;
		var base = new Pane({});
		base.addPane(new Label({x:5, y:2, text:result.address}));
		base.addListener('onclick', function(e) {
			getCamera().setProperties({longitude:loc[0], latitude:loc[1], altitude:10000});
		});
	}
	
	return base;
}

/**
* GSearchFeature
*
* Feature to be placed on the globe for the result. Clicking will zoom camera to location.
*/
function GSearchFeature(plugin, result)
{
	with(plugin)
	{
		var loc = result.Point.coordinates;
		var ftr = new Feature({name:result.address});
		ftr.setIcon(new Icon({href:_placemark_url}));
		ftr.setGeometry(new Point(new LatLonAlt(loc[1], loc[0], loc[2])));
		ftr.addListener("onclick", function(e) {
			getCamera().setProperties({longitude:loc[0], latitude:loc[1], altitude:10000});
		});
	}
	
	return ftr;
}
